var FormulaData = "";


//用于对话框"确定"按钮的回调
var _callBack = function (status) {
    //alert(FormulaData);
    if (status == 1) {
        $("#DataSource").val(FormulaData);
    }
}

if (typeof ($) != "undefined") {
    $(document).ready(function () {
        $("#btnSetFormula").click(function () { //设置表达式
            FormulaData = $("#DataSource").val();
            document.getElementById("EditUIWindow").src = "../EHR/hrSalaryFormula.aspx?flag=0&r=" + Math.random();
            setTimeout(function () {
                $("#dialogArea").fbmodal({
                    title: "公式设置",
                    cancel: "退出",
                    okay: "确定",
                    modal: true,
                    okaybutton: true,
                    cancelbutton: true,
                    buttons: true,
                    opacity: 0.0,
                    fadeout: true,
                    overlayclose: true,
                    modaltop: "15%",
                    modalwidth: "450",
                    modalheight: "270"
                }, _callBack);
            }, 100);
        });
        var flag = getPar1("flag");
        instance[0].FieldType = "varchar";
        var id = getPar1("id");
        if (id == "" && flag != "set") {
            var date = new Date();
            $("#ValidDate").val(date.getFullYear() + "-" + (date.getMonth() + 1) + "-" + date.getDate());
            $("#ExpireDate").val("2030-12-31");
        }
        if (flag == "set") { //为薪资方案设定薪资项
            var solutionId = getPar(decodeURI(location.href), "solutionId");
            $("input,select").attr("disabled", true).css("background", "whitesmoke");
            $("#btnSetFormula").attr("disabled", false)
            if (id == "") //新增
            {
                var itemList = [];
                var obj = $("#divItem");
                obj.html("");
                obj.append("<select id='tempItemName' style='width:405px' title='项目名称'></select>");
                $("#tempItemName").change(function () {
                    var itemId = $("#tempItemName").val();
                    instance[0].ID = Guid.NewGuid().ToString();
                    instance[0].SalarySolutionID = solutionId;
                    for (var i = 0; i < itemList.length; i++) {
                        if (itemList[i].ID == itemId) {
                            instance[0].ItemName = itemList[i].ItemName;
                            $("#ItemNotes").val(itemList[i].ItemNotes);
                            $("#FieldType").val(itemList[i].FieldType);
                            $("#CharValue").val(itemList[i].CharValue);
                            $("#NumValue").val(itemList[i].NumValue);
                            $("#DataSource").val(itemList[i].DataSource);
                            $("#Flag").val(itemList[i].Flag);
                            $("#DataLength").val(itemList[i].DataLength);
                            $("#DotLength").val(itemList[i].DotLength);
                            $("#Sort").val(itemList[i].Sort);
                            $("#ValidDate").val(itemList[i].ValidDate);
                            $("#ExpireDate").val(itemList[i].ExpireDate);
                            break;
                        }
                    }
                });
                //加载原始薪资项，不包含已加载的项
                var url = "../Extend/AjaxData/jsonlist.aspx?entity=salSalaryItem&condition=(SalarySolutionID is null or SalarySolutionID='00000000-0000-0000-0000-000000000000') and ItemName not in(select ItemName from salSalaryItem where SalarySolutionID='" + solutionId + "') &r=" + Math.random();
                $.get(url, function (d, s) {
                    var list = eval("(" + d + ")");
                    var f = false;
                    if (list.total > 0) {
                        for (var o in list.rows) {
                            var t = list.rows[o].ItemName;
                            var v = list.rows[o].ID;
                            if (typeof (t) != "undefined" && typeof (v) != "undefined") {
                                var array = [];
                                array.ID = v;
                                array.ItemName = t,
                                array.SalarySolutionID = list.rows[o].SalarySolutionID;
                                array.ItemNotes = list.rows[o].ItemNotes;
                                array.FieldType = list.rows[o].FieldType;
                                array.CharValue = list.rows[o].CharValue;
                                array.DataLength = typeof (list.rows[o].DataLength) == "undefined" ? 9 : list.rows[o].DataLength;
                                array.DotLength = typeof (list.rows[o].DotLength) == "undefined" ? 2 : list.rows[o].DotLength;
                                array.NumValue = list.rows[o].NumValue;
                                array.DataSoruceType = list.rows[o].DataSoruceType;
                                array.DataSource = list.rows[o].DataSource;
                                array.Sort = typeof (list.rows[o].Sort) == "undefined" ? 0 : list.rows[o].Sort;
                                array.Flag = list.rows[o].Flag;
                                array.AddUser = list.rows[o].AddUser;
                                array.AddTime = list.rows[o].AddTime;
                                array.ValidDate = list.rows[o].ValidDate;
                                array.ExpireDate = list.rows[o].ExpireDate;
                                itemList.push(array);
                                $("#tempItemName").append("<option value='" + v + "' title='" + t + "'>" + t + "</option>");
                                f = true;
                            }
                        }
                    }
                    if (f) {//初始化后触发change事件
                        $("#tempItemName").trigger("change");
                    }
                });
            }
        }
    });
}

//数据验证
var fnCheckData = function () {
    var flag = getPar1("flag");
    var id = getPar1("id");
    var itempName = "";
    if (flag == "set" && id == "") { //为薪资方案设定薪资项
        itempName = $("#tempItemName").val();
        if (itempName == "" || itempName == null) {
            return "请选择薪资项目！";
        }
    }
    else {
        itempName = $("#ItemName").val();
        if (itempName == "" || itempName == null) {
            return "项目名称不能为空！";
        }
    }
    if ($("#DataSource").val() == "") {
        return "请设置计算公式！";
    }
    if (($("#ValidDate").val() == "" || $("#ValidDate").val() == null)) {
        return "生效日期不能为空！";
    }
    if ($("#ExpireDate").val() == "" || $("#ExpireDate").val() == null) {
        return "失效日期不能为空！";
    }
}